﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace CommunitySystem
{
    public partial class OrgControl : UserControl
    {

        SystemService.SystemServiceClient Client = new SystemService.SystemServiceClient();
        SystemService.OrgInfo OrgInfoModel = new SystemService.OrgInfo();

        public OrgControl()
        {
            InitializeComponent();

            LoadData();

        }
        /// <summary>
        /// 加载组织机构数据
        /// </summary>
        void LoadData()
        {
            Client.GetOrgInfoCompleted += new EventHandler<SystemService.GetOrgInfoCompletedEventArgs>(Client_GetOrgInfoCompleted);
            Client.GetOrgInfoAsync();
        }


        /// <summary>
        ///绑定TreeView
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Client_GetOrgInfoCompleted(object sender, SystemService.GetOrgInfoCompletedEventArgs e)
        {
            OrgTree.Items.Clear();
            //获取实体
            ObservableCollection<SystemService.OrgInfo> items = e.Result;
            foreach (var i in items.Where(c => c.PID == 0))
            {
                TreeViewItem item = new TreeViewItem();

                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, Width = 150, HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, VerticalAlignment = System.Windows.VerticalAlignment.Stretch };
                sp.Children.Add(new TextBlock() { Text = i.OrgNo, FontSize = 12, Margin = new Thickness(4, 0, 0, 0) });
                sp.Children.Add(new TextBlock() { Text = i.OrgName, FontSize = 12, Margin = new Thickness(4, 0, 0, 0) });

                item.Header = sp;
                item.DataContext = i;

                AddChildNode(item, i, items);
                //if (i.ID == SelectID)
                //{
                //    item.IsExpanded = true;
                //}
                OrgTree.Items.Add(item);
            }

        }
        /// <summary>
        /// 添加子项目
        /// </summary>
        /// <param name="pitem"></param>
        /// <param name="OrgModel"></param>
        /// <param name="source"></param>
        void AddChildNode(TreeViewItem pitem, SystemService.OrgInfo OrgModel, ObservableCollection<SystemService.OrgInfo> source)
        {
            //添加子项
            foreach (var i in source.Where(c => c.PID == OrgModel.Id))
            {
                TreeViewItem item = new TreeViewItem();
                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, Width = 150, HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, VerticalAlignment = System.Windows.VerticalAlignment.Stretch };
                sp.Children.Add(new TextBlock() { Text = i.OrgNo, FontSize = 12, Margin = new Thickness(4, 0, 0, 0) });
                sp.Children.Add(new TextBlock() { Text = i.OrgName, FontSize = 12, Margin = new Thickness(4, 0, 0, 0) });

                item.Header = sp;

                item.DataContext = i;

                AddChildNode(item, i, source);

                pitem.Items.Add(item);
            }

        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NewData_Click(object sender, RoutedEventArgs e)
        {
            int PID = 0;
            if (OrgTree.SelectedItem == null)
            {
                
            }
            else
            {
                var Model = (OrgTree.SelectedItem as TreeViewItem).DataContext as SystemService.OrgInfo;
                PID = Model.Id;
            }

            OrgManageWindow OMWindow = new OrgManageWindow(PID);
            //注册关闭事件
            OMWindow.Closed += new EventHandler(OMWindow_Closed);

            OMWindow.Show();
        }

        /// <summary>
        /// 关闭后刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OMWindow_Closed(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EditData_Click(object sender, RoutedEventArgs e)
        {
            var Model = (OrgTree.SelectedItem as TreeViewItem).DataContext as SystemService.OrgInfo;

            //throw new NotImplementedException();
            if (Model != null)
            {
                OrgManageWindow OMWindow = new OrgManageWindow(Model);
                ///注册关闭事件
                OMWindow.Closed +=new EventHandler(OMWindow_Closed);
                OMWindow.Show();

            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteData_Click(object sender, RoutedEventArgs e)
        {
            var Model = (OrgTree.SelectedItem as TreeViewItem).DataContext as SystemService.OrgInfo;

            //throw new NotImplementedException();
            if (Model != null)
            {
                Client.DeleteOrgCompleted-= new EventHandler<SystemService.DeleteOrgCompletedEventArgs>(Client_DeleteOrgCompleted);
                Client.DeleteOrgCompleted += new EventHandler<SystemService.DeleteOrgCompletedEventArgs>(Client_DeleteOrgCompleted);
                Client.DeleteOrgAsync(Model.Id);

            }
        }
        /// <summary>
        /// 删除组织机构。。 如果有子节点不能删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Client_DeleteOrgCompleted(object sender, SystemService.DeleteOrgCompletedEventArgs e)
        {
                MessageBox.Show(e.Result.ToString());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OrgTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
             var Model = (OrgTree.SelectedItem as TreeViewItem).DataContext as SystemService.OrgInfo;

            //throw new NotImplementedException();
             if (Model != null)
             {
                 //Client.GetUserInfoByOrgNoCompleted -= new EventHandler<SystemService.GetUserInfoByOrgNoCompletedEventArgs>(Client_GetUserInfoByOrgNoCompleted);
                 Client.GetUserInfoByOrgNoCompleted += new EventHandler<SystemService.GetUserInfoByOrgNoCompletedEventArgs>(Client_GetUserInfoByOrgNoCompleted);
                 Client.GetUserInfoByOrgNoAsync(Model.OrgNo);
             }
        }
        /// <summary>
        /// 绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Client_GetUserInfoByOrgNoCompleted(object sender, SystemService.GetUserInfoByOrgNoCompletedEventArgs e)
        {
            //throw new NotImplementedException();
            DGUser.ItemsSource = e.Result;

        }

    }
}
